<template>
  <el-dialog :title="adminpassword.title" :visible.sync="adminpassword.show" width="600px">
    <el-form :model="parames" :rules="rules" label-width="120px" ref="adminpassword">
      <el-form-item label="原始密码:" prop="originalPwd">
        <el-input
          type="password"
          v-model="parames.originalPwd"
          autocomplete="off"
          placeholder="请输入原始密码"
        ></el-input>
      </el-form-item>
      <el-form-item label="新密码:" prop="newPwd">
        <el-input type="password" v-model="parames.newPwd" autocomplete="off" placeholder="请输入新密码"></el-input>
      </el-form-item>
      <el-form-item label="确认密码:" prop="confirmNewPwd">
        <el-input
          type="password"
          v-model="parames.confirmNewPwd"
          autocomplete="off"
          placeholder="请再次输入新密码"
        ></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="adminpassword.show = false">取 消</el-button>
      <el-button type="primary" @click="handleSend">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.parames.confirmNewPwd !== "") {
          this.$refs.adminpassword.validateField("confirmNewPwd");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.parames.newPwd) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      parames: {
        originalPwd: "",
        newPwd: "",
        confirmNewPwd: ""
      },
      rules: {
        originalPwd: [
          { required: true, message: "请输入原始密码", trigger: "blur" }
        ],
        newPwd: [{ validator: validatePass, trigger: "blur" }],
        confirmNewPwd: [{ validator: validatePass2, trigger: "blur" }]
      }
    };
  },
  props: ["adminpassword"],
  methods: {
    handleClose() {
      this.adminpassword.show = false;
    },
    resetData() {
      this.$refs.adminpassword.resetFields();
    },
    handleSend() {
      this.$refs.adminpassword.validate(valid => {
        if (valid) {
          this.$axios
            .post("/tx/profile/updatePwd", this.parames)
            .then(res => {
              if (res.code === 10200) {
                this.$message({
                  message: "操作成功！",
                  type: "success"
                });
                setTimeout(() => {
                  this.handleClose();
                }, 200);
              }
            });
        }
      });
    }
  },
  computed: {
    changeDialog() {
      return this.adminpassword.show;
    }
  },
  watch: {
    changeDialog(culval) {
      if (culval) {
      } else {
        this.resetData();
      }
    }
  }
};
</script>
<style>
</style>
